package com.pangolin.pangolinim.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.net.Uri;
import android.util.Base64;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lqr.emoji.LQREmotionKit;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.sqlite.DbHelper;
import com.pangolin.pangolinim.utils.DHUtil;
import com.pangolin.pangolinim.utils.SPUtil;

import java.util.HashMap;
import java.util.Map;

public class mainApp extends Application {
    private static Context mContext;

    /* 服务器配置信息 */
    private static String chatHost = "192.168.0.119";
    private static int chatPort = 20000;
    private static String fileServer = "http://192.168.0.119";

    /* 加密字段配置信息 */
    private static String pubkey;
    private static String prikey;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();

        /** 初始化Logger */
        Logger.addLogAdapter(new AndroidLogAdapter());

        /** 安装配置 */
        if(isFirstRun()){
            putKeys();
            SPUtil.put(this , "firstrun" , 1);
        }

        /** 初始化表情控件 */
        LQREmotionKit.init(this, (context, path, imageView) -> Glide.with(context).load(path).centerCrop().diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(imageView));

        /** 读取DH加密算法的公私钥对 */
        getKeys();
    }

    public static Context getContext() { return mContext; }

    public static String getChatHost() { return chatHost; }

    public static int getChatPort() { return chatPort; }

    public static String getFileServer() { return fileServer; }

    public static String getPubkey(){ return pubkey; }

    public static String getPrikey(){ return prikey; }

    /**
     * TODO:创建本程序自己的公私钥对，并写入SharedPreferences
     */
    private static void putKeys(){
        try{
            Map<String, Object> keyMapA = DHUtil.initKey();
            byte publicKeyA[] = DHUtil.getPublicKey(keyMapA);
            byte privateKeyA[] = DHUtil.getPrivateKey(keyMapA);
            SPUtil.put(mContext , "pubkey" , Base64.encodeToString(publicKeyA , Base64.NO_WRAP));
            SPUtil.put(mContext , "prikey" , Base64.encodeToString(privateKeyA , Base64.NO_WRAP));
        }catch (Exception e){
            Toast.makeText(mContext , "DH算法错误："+e.getMessage() , Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * TODO:从SharedPreferences中读取本程序自己的公私钥对
     */
    private static void getKeys(){
        prikey = (String) SPUtil.get(mContext , "prikey" , "");
        pubkey = (String) SPUtil.get(mContext, "pubkey" , "");
        if(prikey.equals("") || pubkey.equals("")){
            Toast.makeText(mContext , "配置信息：加密密钥对丢失" , Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * TODO:检测是否是程序首次运行
     * @return true-首次运行；false-非首次运行
     */
    private static boolean isFirstRun(){
        return ((int) SPUtil.get(mainApp.getContext() , "firstrun" , 0) == 0 ? true : false);
    }
}
